<?php
//对象的封装
class Staff
{
	private $name;
	private $age;
	private $salary;
	public function __construct($name, $age, $salary)
	{
		//构造方法用来初始化对象属性
		$this->name = $name;
		$this->age = $age;
		$this->salary = $salary;
	}
	/*  这样的方法太麻烦了要写6个
	public function getName()
	{
		return $this->name;//私有属性不能直接访问，可以通过方法间接访问
	}
	public function getSalary()
	{
		return $this->salary;
	}
	public function getAge()
	{
		return $this->age;
	}

	public function setName($name, $value)//通过方法去修改私有属性的值，传两个参数，一个是属性姓，别一个要改为的值 
	{
		$this->$name = $value;
	}
	public function setAge($name, $value)
	{
		$this->$name = $value;
	}
	public function setSalary($name, $value)
	{
		$this->$name = $value;
	}*/

	public function __get($name)//直接用魔术方法__get 一个就搞定了
	{
	 return	$this->$name;
	}
	public function __set($name, $value)//直接用魔术方法__set 就可以改值了。
	
	{
		//用这个方式我们可以控制某些属性的值不能改，如年龄。
		if ($name == 'age') {
			return false;
		}
		return $this->$name = $value;

	}
	//__isset()检测是否存在某个属性
	public function __isset($name)
	{
		return isset($this->$name);
	}

	public function __unset($name)//这样才能删除
	{
		unset($this->$name);
	}

}

$obj = new Staff('Peter', 28, 4500);
/* 
echo $obj->getName();
echo $obj->getSalary();
echo $obj->getAge();
echo "<hr>";
$obj->setName('name', 'yuping');
echo $obj->getName();
echo "<hr>";
$obj->setAge('age', 36);
echo $obj->getAge();
echo "<hr>";
$obj->setSalary('salary', 6800);
echo $obj->getSalary();
*/
echo $obj->name;//和public属性一样的访问了
echo $obj->salary;
echo $obj->age;
echo "<hr>";
$obj->name = 'yuping';
echo $obj->name;
echo "<hr>";
$obj->age = 36;//年龄没有改
echo $obj->age;
echo "<hr>";
$obj->salary = 6800;
echo $obj->salary;
echo "<hr>";
echo isset($obj->name) ? '存在' : '不存在';//如要不用__isset 就检测不到，
echo "<hr>";
unset($obj->age);
echo $obj->age;